home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / bm_cpio.gz / unixbm.cpio / unix.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  3KB  |  159 lines

  1. /* This file contains machine specific functions */
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/termio.h>
  6. #include <signal.h>
  7. #include <string.h>
  8. #include "bm.h"
  9. #include "ndir.h"
  10.  
  11. struct termio    savetty;
  12. int    savedtty = 0;        /* tell weather savetty is valid */
  13.  
  14. /* This function should put the tty in a mode such that signgle characters
  15. * can be read without waiting for a complete line. Echo should be on.
  16. */
  17. setrawmode()
  18. {
  19.     struct termio ttybuf;
  20.     ioctl(0, TCGETA, &ttybuf);
  21.     ioctl(0, TCGETA, &savetty);
  22.     savedtty = 1;
  23.     ttybuf.c_lflag &= ~ICANON;
  24.     ttybuf.c_iflag &= ~ICRNL;
  25.     ttybuf.c_oflag &= ~OCRNL;
  26.     ttybuf.c_cc[VTIME] = '\0';
  27.     ttybuf.c_cc[VMIN] = '\1';
  28.     ioctl(0, TCSETA, &ttybuf);
  29. }
  30.  
  31. /* This function should restore the tty modes back to cooked mode */
  32. setcookedmode()
  33. {
  34.     if (savedtty) {
  35.         ioctl(0, TCSETA, &savetty);
  36.         savedtty = 0;
  37.     }
  38. }
  39.  
  40. /* This function return one charater form the keyboard. It will wait
  41. * for a character to be input. This function will echo the character.
  42. * This funtion will return afer each character is typed if raw is set
  43. */
  44. int
  45. getrch()
  46. {
  47.     int    c;
  48.     c = getchar();
  49.     return c & 0xff;
  50. }
  51.  
  52. /* This function show clear screen and put cursor at top of screen */
  53. screen_clear()
  54. {
  55. }
  56.  
  57. setsignals()
  58. {
  59.     /* ignore break */
  60.     signal(SIGINT,SIG_IGN);
  61. }
  62.  
  63. /* wildcard filename lookup */
  64. filedir(name, times, ret_str)
  65. char    *name;
  66. int    times;
  67. char    *ret_str;
  68. {
  69.     static char    dname[128], fname[128];
  70.     static DIR *dirp = NULL;
  71.     struct direct *dp;
  72.     struct stat sbuf;
  73.     char    *cp, temp[128];
  74.  
  75.     /*
  76.      * Make sure that the NULL is there in case we don't find anything
  77.      */
  78.     ret_str[0] = '\0';
  79.  
  80.     if (times == 0) {
  81.         /* default a null name to *.* */
  82.         if (name == NULL || *name == '\0')
  83.             name = "*.*";
  84.         /* split path into directory and filename */
  85.         if ((cp = strrchr(name, '/')) == NULL) {
  86.             strcpy(dname, ".");
  87.             strcpy(fname, name);
  88.         } else {
  89.             strcpy(dname, name);
  90.             dname[cp - name] = '\0';
  91.             strcpy(fname, cp + 1);
  92.             /* root directory */
  93.             if (dname[0] == '\0')
  94.                 strcpy(dname, "/");
  95.             /* trailing '/' */
  96.             if (fname[0] == '\0')
  97.                 strcpy(fname, "*.*");
  98.         }
  99.         /* close directory left over from another call */
  100.         if (dirp != NULL)
  101.             closedir(dirp);
  102.         /* open directory */
  103.         if ((dirp = opendir(dname)) == NULL) {
  104.             printf("Could not open DIR (%s)\n", dname);
  105.             return;
  106.         }
  107.     } else {
  108.         /* for people who don't check return values */
  109.         if (dirp == NULL)
  110.             return;
  111.     }
  112.  
  113.     /* scan directory */
  114.     while ((dp = readdir(dirp)) != NULL) {
  115.         /* test for name match */
  116.         if (wildmat(dp->d_name, fname)) {
  117.             /* test for regular file */
  118.             sprintf(temp, "%s/%s", dname, dp->d_name);
  119.             if (stat(temp, &sbuf) < 0)
  120.                 continue;
  121.             if ((sbuf.st_mode & S_IFMT) != S_IFREG)
  122.                 continue;
  123.             strcpy(ret_str, dp->d_name);
  124.             break;
  125.         }
  126.     }
  127.  
  128.     /* close directory if we hit the end */
  129.     if (dp == NULL) {
  130.         closedir(dirp);
  131.         dirp = NULL;
  132.     }
  133. }
  134.  
  135.  
  136. /*
  137. **    strcmpic: string compare, ignore case
  138. */
  139.  
  140. stricmp(s1, s2)
  141. char *s1, *s2;
  142. {
  143.     register char *u = s1;
  144.     register char *p = s2;
  145.  
  146.     while(*p != '\0') {
  147.         /* chars match or only case different */
  148.         if(tolower(*u) == tolower(*p)) {
  149.             p++;    /* examine next char */
  150.             u++;
  151.         } else {
  152.             break;    /* no match - stop comparison */
  153.         }
  154.     }
  155.  
  156.     return(tolower(*u) - tolower(*p)); /* return "difference" */
  157. }
  158.  
  159.